package com.thinkjoy.util;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by shaojun on 8.25 025.
 */
public class ConsoleHandle {

    /**
     * 控制台输入
     */
    private static BufferedReader inputLine = new BufferedReader(new InputStreamReader(System.in));

    /**
     * 获取控制台输入
     *
     * @return
     */
    public static BufferedReader getInputLine() {
        return inputLine;
    }

    /**
     * 输入路径
     */
    public static String inputDirectoryPath() {
        String result = null;
        try {
            while (!ConsoleHandle.validDirectory(result)) {
                result = inputLine.readLine();
                System.out.println("您的输入为:" + result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    /**
     * 验证路径
     *
     * @param path
     * @return
     */
    public static boolean validDirectory(String path) {
        if (StringUtils.isBlank(path))
            return false;

        boolean result = false;
        File exsitFile = new File(path);
        if (!exsitFile.isAbsolute()) {
            System.out.println("输入并不是绝对路径！请重新输入 ：");
            return false;
        } else if (!exsitFile.exists()) {
            System.out.println("文件夹不存在！请重新输入 ：");
        } else if (exsitFile.isFile()) {
            System.out.println("输入的并不是个文件夹！请重新输入 ：");
        } else if (exsitFile.isDirectory()) {
            result = true;
        }
        return result;
    }

}
